package com.example.musicplayer.broadcastreceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.Log;
import android.widget.RemoteViews;

import androidx.annotation.RequiresApi;

import com.example.musicplayer.R;
import com.example.musicplayer.entity.Music;
import com.example.musicplayer.service.MusicService;
import com.example.musicplayer.util.MusicNotification;

public class NotificationReceiver extends BroadcastReceiver {

    private RemoteViews mSmallRemoteViews;
    private RemoteViews mLargeRemoteViews;
    private MusicService.MusicBinder mMusicService;


    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void onReceive(Context context, Intent intent) {
        mMusicService = MusicService.getMusicService(); // 获取音乐服务
        MusicNotification mMusicNotification = mMusicService.getMusicNotification();
        mSmallRemoteViews = mMusicNotification.getSmallRemoteViews();
        mLargeRemoteViews = mMusicNotification.getLargeRemoteViews();
        String action = intent.getAction();
        if (MusicNotification.ACTION_PREVIOUS.equals(action)) { // 点击了“上一首”按钮
            Log.i("--------------------", "music:接收到跳转到上一首的广播");
            mMusicService.skipToPreviousSong(); // 跳转到上一首音乐
            setNotificationView();
        } else if (MusicNotification.ACTION_START_OR_PAUSE.equals(action)) { // 点击了暂停或播放按钮
            Log.i("==================", "点击了播放暂停按钮");
            if (mMusicService.isPlaying()) { // 如果正在播放
                mMusicService.pauseMusic(); // 暂停音乐
                // 将播放暂停按钮更改为播放
                mSmallRemoteViews.setImageViewResource(R.id.notification_start_or_pause, R.drawable.ic_action_start_play);
                // 将播放暂停按钮更改为播放
                mLargeRemoteViews.setImageViewResource(R.id.notification_start_or_pause, R.drawable.ic_action_start_play);
                // 更新通知栏
                mMusicNotification.updateNotification(MusicNotification.NOTIFICATION_ID);
            } else { // 如果处于暂停状态
                mMusicService.startMusic(); // 播放音乐
                // 将播放暂停按钮更改为暂停按钮
                mSmallRemoteViews.setImageViewResource(R.id.notification_start_or_pause, R.drawable.ic_action_stop_play);
                mMusicService.startMusic(); // 播放音乐
                // 将播放暂停按钮更改为暂停按钮
                mLargeRemoteViews.setImageViewResource(R.id.notification_start_or_pause, R.drawable.ic_action_stop_play);
                // 更新通知栏
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    mMusicService.updateNotification(MusicNotification.NOTIFICATION_ID);
                }
            }
        } else if (MusicNotification.ACTION_NEXT.equals(action)) { // 点击了“下一首”按钮
            Log.i("--------------------", "music:接收到跳转到下一首的广播");
            mMusicService.skipToNextSong(); // 跳转到下一首音乐
            setNotificationView();
        } else if (MusicNotification.ACTION_LIST.equals(action)) {
            Log.i("==================", "点击了某个列表");
            setNotificationView();
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private void setNotificationView() {
        Music music = mMusicService.getCurrentMusic();
        mSmallRemoteViews.setImageViewBitmap(R.id.notification_album_img, music.getAlbumArt()); // 设置专辑封面
        // 设置歌曲名称
        mSmallRemoteViews.setTextViewText(R.id.notification_music_name, music.getName());
        // 设置歌手名称
        mSmallRemoteViews.setTextViewText(R.id.notification_artist_name, music.getArtist());
        // 将播放按钮设置为暂停按钮
        mSmallRemoteViews.setImageViewResource(R.id.notification_start_or_pause, R.drawable.ic_action_stop_play);
        mLargeRemoteViews.setImageViewBitmap(R.id.notification_album_img, music.getAlbumArt()); // 设置专辑封面
        // 设置歌曲名称
        mLargeRemoteViews.setTextViewText(R.id.notification_music_name, music.getName());
        // 设置歌手名称
        mLargeRemoteViews.setTextViewText(R.id.notification_artist_name, music.getArtist());
        // 将播放按钮设置为暂停按钮
        mLargeRemoteViews.setImageViewResource(R.id.notification_start_or_pause, R.drawable.ic_action_stop_play);
        // 更新通知栏
        mMusicService.updateNotification(MusicNotification.NOTIFICATION_ID);
        Log.i("--------------------", "music:设置通知栏样式");

    }
}
